package com.pai4j.common.service;

import com.chat4j.common.enums.RedisKeyEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: 程序员七七
 * @Date: 31.12.21 11:05 下午
 * 上传进度缓存服务
 */
@Slf4j
@Service
public class PutObjectProgressCacheService {

    @Autowired
    private RedisService redisService;

    /***
     * 设置进度 任务ID
     * @param taskId
     * @param percent 进度 10 、20 、…… 100
     */
    public void set(String taskId, int percent) {
        String key = RedisKeyEnum.TASK_PROGRESS_CACHE.getKey(taskId);
        redisService.set(key, String.valueOf(percent), RedisKeyEnum.TASK_PROGRESS_CACHE.getExpireTime());
        log.info("任务 {} 当前进度 {}%", taskId, percent);
    }

    /***
     * 查询进度
     * @param taskId 任务ID
     */
    public Integer get(String taskId) {
        String key = RedisKeyEnum.TASK_PROGRESS_CACHE.getKey(taskId);
        String percent = redisService.get(key);
        return StringUtils.isNotBlank(percent) ? Integer.valueOf(percent) : 0;
    }
}
